package com.neu.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.neu.dao.TeacherDao;
import com.neu.entity.TBUserRole;
import com.neu.entity.Teacher;
import com.neu.util.JDBCUtilTeacher;

public class TeacherDaoImpl implements TeacherDao {
private Connection conn;

/**
 *  增加教师信息
 * @throws  
 */
	@Override
	public boolean addTeacher(Teacher tec) throws SQLException {
		//  创建Connection对象   也是第一部  然后抛出异常
		Connection conn =null;
		try {
			conn = JDBCUtilTeacher.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//  第二步    就是生成  sql 的语句  
		String sql = "insert into teacher value(null,?)";
		//  第三部    生成  sql 的执行器:  把 sql 的语句放到执行器里面
		PreparedStatement ps = conn.prepareStatement(sql);
		//  第四部   赋值
		ps.setString(1, tec.getName());
		//   因为是增加   所以用 
		int a = ps.executeUpdate();
		if(a>0) {
			return true;
		}
		return false;
		
		
//		Connection conn=null;
//		try {
//			 conn = JDBCUtilTeacher.getConn();
//		} catch (ClassNotFoundException | SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		String sql = "insert into teacher value(null,?)";
//		PreparedStatement ps=conn.prepareStatement(sql);
//		ps.setString(1, tec.getName());
//		int a = ps.executeUpdate();
//		if(a>0) {
//			return true;
//		}
//		return false;
	}

	/**
	 *   删除教师信息:
	 * @throws ClassNotFoundException 
	 */
	@Override
	public boolean deleteTeacher(Teacher tec) throws SQLException {
		//获取Connection对象，通过工具类  
		//   JDBCUtil  
		Connection conn=null;
		 boolean flag = false;
	
		try {
			conn = JDBCUtilTeacher.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql="select * from cource where teacher_id=?";
		//生成执行器
		PreparedStatement ps=conn.prepareStatement(sql);
		//生成结果集，对结果进行处理
		ps.setInt(1,tec.getId());
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
		String sq2="delete from studentcource where cource_id in(select id from cource where teacher_id=?)";
		PreparedStatement ps1=conn.prepareStatement(sq2);
		ps1.setInt(1,tec.getId());
		int a=ps1.executeUpdate();
		if(a>0) {
		String sql3="delete  from cource where teacher_id=?";
		PreparedStatement ps2=conn.prepareStatement(sql3);
		ps2.setInt(1,tec.getId());
		int b=ps2.executeUpdate();
		if(b>0) {
				String sql4="delete  from teacher where id=?";
				PreparedStatement ps4=conn.prepareStatement(sql4);
				ps4.setInt(1,tec.getId());
				int d=ps4.executeUpdate();
				if(d>0) {
					System.out.println("删除成功");
					flag=true;
				}
			}
		}
	}else {
		String sql4="delete  from teacher where id=?";
		PreparedStatement ps4=conn.prepareStatement(sql4);
		ps4.setInt(1,tec.getId());
		int d=ps4.executeUpdate();
		if(d>0) {
			System.out.println("删除成功");
			flag=true;
		}	
	}
		return flag;
}
/**
 * 查询全部教师信息
 */
//	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public  List<Teacher> selectTeachers() throws SQLException {
//		Connection conn =null;
//		try {
//		 conn = JDBCUtilTeacher.getConn();
//		} catch (ClassNotFoundException | SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		String sql ="select*from student";
//		PreparedStatement ps = conn.prepareStatement(sql);
//		List<Teacher> lise = new ArrayList();
//		ResultSet rs = ps.executeQuery();
//		if(rs.next()) {
//			lise.add(new Teacher(rs.getInt(1), rs.getString(2)));
//		}
//		
//		return lise;
		
		
		
		
		Connection conn=null;
		try {
		 conn = JDBCUtilTeacher.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select*from teacher ";
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<Teacher> list_stu = new ArrayList();
		while(rs.next()) {
			list_stu.add(new Teacher(rs.getInt(1),rs.getString(2)));
		}
		return list_stu;

	}

	//  查询单一的教师信息
	@Override
	public Teacher finfByNameOrId(Teacher tce) throws SQLException {
		
		
		Connection conn=null;
		try {
			 conn = JDBCUtilTeacher.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql ="select*from teacher where id=? or name=?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, tce.getId());
		ps.setString(2, tce.getName());
		ResultSet rs = ps.executeQuery();
//		Teacher tea = null;
		if(rs.next()) {
			tce=new Teacher(rs.getInt(1),rs.getString(2));
		}
		return tce;
	}
//  修改教师信息  
	@Override
	public boolean updateTeacher(Teacher tce) throws SQLException {
		Connection conn=null;
		try {
			 conn = JDBCUtilTeacher.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			
			e.printStackTrace();
		}
		String sql ="update teacher set name=? where id=?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1,tce.getName());
		ps.setInt(2, tce.getId());
		int a = ps.executeUpdate();
		if(a>0) {
			return true;
		}
		return false;
	}

}
//  修改教师信息  
// @Override
// public boolean updateTeacher(Teacher tce) throws SQLException {
//  TBUserRole userRole = new TBUserRole();
//  if(tce.getId() == userRole.getId()) {
//   Connection conn=null;
//   try {
//    conn = JDBCUtilTeacher.getConn();
//   } catch (ClassNotFoundException | SQLException e) {
//    
//    e.printStackTrace();
//   }
//   String sql ="update teacher set name=? where id=?";
//   PreparedStatement ps = conn.prepareStatement(sql);
//   ps.setString(1,tce.getName());
//   ps.setInt(2, tce.getId());
//   int a = ps.executeUpdate();
//   if(a>0) {
//    return true;
//   }
//   return false;
//  }else {
//   System.out.println("非本老师无法修改！");
//  }
//return false;
//  }
//}
